<?php
class ArticleAction extends Action {

    public function _initialize() {
        $Config = A('Config');
        $Config->checkLogIn();
        $Config->checkUserLevel(array('1', '2', '3'));
    }
    
    public function index() {
        $Article = M('Article');
        import("ORG.Util.Page");
        
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $count = $Article->where(array('cat_id' => $id))->count();
            $Page = new Page($count, 25);
            $article = $Article->where(array('cat_id' => $id))->order(array('add_time' => 'desc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        } else {
            $count = $Article->count();
            $Page = new Page($count, 25);
            $article = $Article->order(array('add_time' => 'desc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }
        
        $this->assign('cat_id', $id);
        $this->assign('article', $article);
        
        $show = $Page->show();
        $this->assign('page',$show);
        
        $this->display();
    }
    
    public function add() {
        $cat_option = '';
        $Category = M('Category');
        // $cat_parent = $Category->where(array('parent_id' => 0))->order(array('list_order' => 'asc'))->select();
        // foreach($cat_parent as $parent_value) {
            // $cat_option .= '<optgroup label="' . $parent_value['cat_name'] . '">';
            // $cat_child = $Category->where(array('parent_id' => $parent_value['cat_id']))->order(array('list_order' => 'asc'))->select();
            // foreach($cat_child as $child_value) {
                // $cat_option .= '<option value="' . $child_value['cat_id'] . '">' . $child_value['cat_name'] . '</option>';
            // }
            // $cat_option .= '</optgroup>';
        // }
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $category = $Category->where(array('cat_id' => $id))->find();
            $this->assign('category', $category);
        } else {
            $id = 0;
            $category = $Category->where(array('cat_type_id' => 1))->order(array('list_order' => 'asc', 'cat_id' => 'asc'))->select();
            foreach ($category as $cat_value) {
                $cat_option .= '<option value="' . $cat_value['cat_id'] . '">' . $cat_value['cat_name'] . '</option>';
            }
            $this->assign('cat_option', $cat_option);
        }

        $this->assign('cat_id', $id);
        $this->display();
    }
    
    public function insert() {
        if (isset($_POST['submitted'])) {
        
            $Config = A('Config');
            $Config->checkTeacherArticle();
            
            $date_now = date('Ymd', time());
            ini_set('memory_limit','-1');
            import('ORG.Net.UploadFile');
            import('ORG.Util.CreatMiniature');                                                                              // 缩略图类
            $upload = new UploadFile();                                                                                      // 实例化上传类
            $upload->maxSize = 5242880;                                                                                // 设置附件上传大小 5M
            $upload->allowExts = array('jpg', 'gif', 'bmp', 'jpeg');                                                // 设置附件上传类型
            $upload->savePath = './Public/Uploads/thumb/' . $date_now . '/';                            // 设置附件上传目录
            $upload->saveRule = uniqid;
            // $upload->thumb = true;
            // $upload->thumbRemoveOrigin = true;
            // $upload->thumbPrefix = 'thumb_';
            // $upload->thumbMaxWidth = 604;
            // $upload->thumbMaxHeight = 279;
            if (!($up = $upload->upload())) {               // 上传错误 提示错误信息
                // $_SESSION['msg'] = $_FILES;
                if ($upload->getErrorMsg() != '没有选择上传文件') {
                    $this->error($upload->getErrorMsg());
                }
            } else {                                                                                                                    // 上传成功  获取上传文件信息
                $info = $upload->getUploadFileInfo();
            }

            
            $Article = D('Article');
            if ($art = $Article->create()) {
                if ($up) {
                    $thumb_path = './Public/Uploads/thumb/' . $date_now . '/';
                    $thumb_url = $thumb_path . $info[0]['savename'];
                    $Thumb = new CreatMiniature();
                    $Thumb->SetVar($thumb_url, 'file');
                    $Thumb->Cut($thumb_url, 604, 279);
                    
                    $Article->thumb = $date_now . '/' . $info[0]["savename"];
                }
                $Article->user_id = $_SESSION['user']['user_id'];
                if ($Article->add()) {
                    $this->success('成功添加文章！');
                } else {
                    $this->error('添加文章失败！');
                }
            } else {
                $this->error($Article->getError());
            }
        }
    }
    
    public function edit() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Article = M('Article');
            $article = $Article->find($id);
            
            $category = $Article->join('econ_category ON econ_article.cat_id = econ_category.cat_id')->where(array('econ_article.article_id' => $id))->find();
            $this->assign('category', $category);
            
            $this->assign('article', $article);
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
    public function update() {
        if (isset($_POST['submitted'])) {
        
            $Config = A('Config');
            $Config->checkTeacherArticle();
            
            $date_now = date('Ymd', time());
            ini_set('memory_limit','-1');
            import('ORG.Net.UploadFile');
            import('ORG.Util.CreatMiniature');                                                                              // 缩略图类
            $upload = new UploadFile();                                                                                      // 实例化上传类
            $upload->maxSize = 5242880;                                                                                // 设置附件上传大小 5M
            $upload->allowExts = array('jpg', 'gif', 'bmp', 'jpeg');                                                // 设置附件上传类型
            $upload->savePath = './Public/Uploads/thumb/' . $date_now . '/';                            // 设置附件上传目录
            $upload->saveRule = uniqid;
            // $upload->thumb = true;
            // $upload->thumbRemoveOrigin = true;
            // $upload->thumbPrefix = 'thumb_';
            // $upload->thumbMaxWidth = 604;
            // $upload->thumbMaxHeight = 279;
            if (!($up = $upload->upload())) {               // 上传错误 提示错误信息
                // $_SESSION['msg'] = $_FILES;
                if ($upload->getErrorMsg() != '没有选择上传文件') {
                    $this->error($upload->getErrorMsg());
                }
            } else {                                                                                                                    // 上传成功  获取上传文件信息
                $info = $upload->getUploadFileInfo();
            }
            
            $Article = D('Article');
            if ($Article->create()) {
                if ($up) {
                    $thumb_path = './Public/Uploads/thumb/' . $date_now . '/';
                    $thumb_url = $thumb_path . $info[0]['savename'];
                    $Thumb = new CreatMiniature();
                    $Thumb->SetVar($thumb_url, 'file');
                    $Thumb->Cut($thumb_url, 604, 279);
                    
                    $Article->thumb = $date_now . '/' . $info[0]["savename"];
                }
                if (false !== $Article->save()) {
                    $this->success('成功编辑文章！');
                } else {
                    $this->error('编辑文章失败！');
                }
            } else {
                $this->error($Article->getError());
            }
        }
    }
    
    public function delete() {
        if (isset($_GET['id'])) {
        
            $Config = A('Config');
            $Config->checkTeacherArticle();
            
            $id = (int) $_GET['id'];
            $Article = M('Article');
            
            if ($Article->delete($id)) {
                $this->success('成功删除文章！');
            } else {
                $this->error('删除文章失败！');
            }
        }
    }
    
}
?>